package bc.swim2.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.naming.Context;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bc.swim2.ejb.entity.Ability;
import bc.swim2.ejb.entity.AbilityRequest;
import bc.swim2.ejb.entity.RegisteredUser;
import bc.swim2.ejb.session.AbilityManagerBeanRemote;
import bc.swim2.ejb.session.AbilityRequestManagerBeanRemote;
import bc.swim2.ejb.session.UserManagerBeanRemote;

/**
 * Servlet implementation class AbilityServlet
 */
public class AbilityServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AbilityServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		if (request.getParameter("methodName").equals("add")) {
			addAbility(request, response);
		}

		else if (request.getParameter("methodName").equals("remove")) {
			removeAbility(request, response);
		}

		else if (request.getParameter("methodName").equals("userRemove")) {
			userRemoveAbility(request, response);
		}

		else if (request.getParameter("methodName").equals("userDeclare")) {
			userDeclareAbility(request, response);
		}

	}

	private void addAbility(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			HttpSession session = request.getSession(true);
			Context ctx = (Context) session.getAttribute("ctx");

			String newAbility = request.getParameter("name");

			Object ref2 = ctx.lookup("AbilityManagerBean/remote");
			AbilityManagerBeanRemote am = (AbilityManagerBeanRemote) ref2;

			/*
			 * Aggiunto un Ability request manager bean per permettere
			 * l'aggiunta direttamente dall'amministratore
			 */

			Object ref3 = ctx.lookup("AbilityRequestManagerBean/remote");
			AbilityRequestManagerBeanRemote arm = (AbilityRequestManagerBeanRemote) ref3;

			if (am.existsAbilityWithName(newAbility)) {

				session.setAttribute("abilityAlreadyExists", true);
			} else {
				am.insertAbility(newAbility);
				List<AbilityRequest> requestList = arm.getAbilityRequestList();
				for (AbilityRequest ar : requestList)
					if (ar.getName().equals(newAbility))
						arm.removeAbilityRequest(ar.getId());
			}
			response.sendRedirect("MainServlet?to=AdminHome");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void removeAbility(HttpServletRequest request,
			HttpServletResponse response) {

		try {
			HttpSession session = request.getSession(true);
			Context ctx = (Context) session.getAttribute("ctx");

			int abilityId = Integer.parseInt(request.getParameter("abilityId"));
			Object ref3 = ctx.lookup("AbilityManagerBean/remote");
			AbilityManagerBeanRemote am = (AbilityManagerBeanRemote) ref3;
			am.removeAbility(abilityId);

			response.sendRedirect("MainServlet?to=AdminHome");

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	private void userRemoveAbility(HttpServletRequest request,
			HttpServletResponse response) {

		int abilityId = Integer.parseInt(request.getParameter("abilityId"));

		try {
			HttpSession session = request.getSession(true);
			Context ctx = (Context) session.getAttribute("ctx");

			Object ref4 = ctx.lookup("UserManagerBean/remote");
			UserManagerBeanRemote um = (UserManagerBeanRemote) ref4;

			RegisteredUser ru = (RegisteredUser) session
					.getAttribute("currentSessionUser");
			um.removeAbility(ru.getId(), abilityId);

			response.sendRedirect("MainServlet?to=MyAbilities");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void userDeclareAbility(HttpServletRequest request,
			HttpServletResponse response) {

		Ability a = null;
		int abilityId = Integer.parseInt(request
				.getParameter("selectedAbilityId"));
		RegisteredUser ru = null;

		try {
			HttpSession session = request.getSession(true);
			Context ctx = (Context) session.getAttribute("ctx");

			Object ref3 = ctx.lookup("AbilityManagerBean/remote");
			AbilityManagerBeanRemote am = (AbilityManagerBeanRemote) ref3;

			Object ref4 = ctx.lookup("UserManagerBean/remote");
			UserManagerBeanRemote um = (UserManagerBeanRemote) ref4;

			a = am.getAbilityById(abilityId);
			ru = (RegisteredUser) session.getAttribute("currentSessionUser");
			um.declareAbility(ru, a);

			response.sendRedirect("MainServlet?to=MyAbilities");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
